package com.tanhua.app.controller;

import com.tanhua.app.Service.SettingsService;
import com.tanhua.app.Service.UserInfoService;
import com.tanhua.app.interceptor.UserThreadLocal;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.pojo.Question;
import com.tanhua.model.pojo.Settings;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private SettingsService settingsService;

    /**
     * 查询用户基础信息
     *
     * @param userID
     */
    @GetMapping
    public ResponseEntity userInfo(Long userID) throws InvocationTargetException, IllegalAccessException {

        if (userID == null) {
            userID = UserThreadLocal.getid();

        }
        UserInfoVo userInfoVo = userInfoService.findById(userID);
        System.out.println(userInfoVo);
        return ResponseEntity.ok(userInfoVo);
    }

    //根据id修改用户详细信息
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {

        userInfoService.update(userInfo);

        return ResponseEntity.ok(null);
    }

    //用户通用设置 - 读取
    //GET/users/settings
    @GetMapping("/settings")
    public ResponseEntity settings() {
   SettingsVo vo= settingsService.settings();
        return ResponseEntity.ok(vo);

    }
    ///post  users/notifications/setting
    //通知设置 - 保存
    @PostMapping("/notifications/setting")
    public ResponseEntity  notifications(@RequestBody Settings settings){
        settingsService.notifications(settings);
        return ResponseEntity.ok(null);

    }
    //POST/users/questions
    //设置陌生人问题 - 保存
    @PostMapping("/questions")
    public ResponseEntity  questions(@RequestBody Map map){
        String content = map.get("content").toString();
        Question question = new Question();
               question.setTxt(content);

        settingsService.questions(question);
        return ResponseEntity.ok(null);
    }

    /**
     * GET users/blacklist
     *黑名单 - 翻页列表
     *
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") int  page ,@RequestParam(defaultValue = "10") int pageSize){
     PageResult<UserInfo> resultPage=settingsService.blacklist(page,pageSize);
     return ResponseEntity.ok(resultPage);
    }

    //DELETE/users/blacklist/:uid
    //黑名单 - 移除
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity delete(@PathVariable("uid") Long userId){
        settingsService.delete(userId);
        return ResponseEntity.ok(null);

    }

    //POST/users/phone/sendVerificationCode
    //修改手机号- 1 发送短信验证码
   /* @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){

    }*/
}
